package com.seanLab.service;

import com.seanLab.domain.*;
import com.seanLab.dto.FeedbackDto.*;
import com.seanLab.repository.*;
import com.seanLab.tool.validator.FeedbackDtoValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ailly on 17-9-22.
 */
@Component
public class FeedbackService {
    @Autowired
    private EditorSessionRepo editorSessionRepo;

    @Autowired
    private EditorFeedbackRepo editorFeedbackRepo;

    @Autowired
    private EditorAddTagRepo editorAddTagRepo;

    @Autowired
    private EditorDeleteTagRepo editorDeleteTagRepo;

    @Autowired
    private EditorImageScoreRepo editorImageScoreRepo;

    @Autowired
    private FeedbackDtoValidator fv;

    public Body addFeedback(EditorFeedbackDto editorFeedbackDto){
        //TODO 检测Dto完整性：sessionID,imgID等

        if (editorFeedbackDto.getAcceptFlag() == 1) {
            int errorType = fv.validateFeedback(editorFeedbackDto);
            if (errorType == 0) {
                //generate entities
                EditorFeedback editorFeedback = FeedbackDto2Domain.transferEditorFeedback(editorFeedbackDto);
                EditorSession updateEditorSession = this.editorSessionRepo.findBySessionID(editorFeedback.getSessionID());

                updateEditorSession.setAcceptFlag(editorFeedback.getAcceptFlag());
                updateEditorSession.setIsTestFlag(editorFeedback.getIsTestFlag());
                updateEditorSession.setArticleCategory(editorFeedback.getArticleCategory());

                List<Integer> tmpSNs = new ArrayList<>();
                updateEditorSession.setAcceptCount(updateEditorSession.getAcceptCount() + 1);

                int top1Flag = 0;
                int top5Flag = 0;

                for (String adoptedImg : editorFeedback.getAdoptedImgs()) {
                    int imgSN = updateEditorSession.getRecommendedImgs().indexOf(adoptedImg);
                    tmpSNs.add(imgSN);
                    if (imgSN == 0) {
                        top1Flag = 1;
                    }
                    if (imgSN < 5) {
                        top5Flag = 1;
                    }
                }

                updateEditorSession.setTop1AcceptFlag(top1Flag);
                editorFeedback.setTop1AcceptFlag(top1Flag);
                updateEditorSession.setTop5AcceptFlag(top5Flag);
                editorFeedback.setTop5AcceptFlag(top5Flag);

                //save
                updateEditorSession.setAdoptedImgs(editorFeedback.getAdoptedImgs());
                updateEditorSession.setAdoptedImgSNs(tmpSNs);

                editorFeedback.setAdoptedImgSNs(tmpSNs);

                this.editorSessionRepo.save(updateEditorSession);
                this.editorFeedbackRepo.save(editorFeedback);

                return Body.getResponseBody(errorType);
            } else {
                return Body.getResponseBody(errorType);
            }
        } else if (editorFeedbackDto.getAcceptFlag() == -1 ){
            //generate entities
            EditorFeedback editorFeedback = FeedbackDto2Domain.transferEditorFeedback(editorFeedbackDto);
            EditorSession updateEditorSession = this.editorSessionRepo.findBySessionID(editorFeedback.getSessionID());

            if (updateEditorSession != null) {
                updateEditorSession.setAcceptFlag(editorFeedback.getAcceptFlag());
                updateEditorSession.setIsTestFlag(editorFeedback.getIsTestFlag());
                updateEditorSession.setArticleCategory(editorFeedback.getArticleCategory());
                this.editorSessionRepo.save(updateEditorSession);
                this.editorFeedbackRepo.save(editorFeedback);

                return Body.getResponseBody(0);
            } else {
                return Body.getResponseBody(24);
            }
        } else {
            return Body.getResponseBody(24);
        }
    }

    //增加某一图片标签
    public Body addTag(EditorAddTagDto editorAddTagDto) {
        int errorType = fv.validateAddTag(editorAddTagDto);

        if (errorType == 0) {
            EditorAddTag editorAddTag = FeedbackDto2Domain.transferEditorAddTag(editorAddTagDto);
            this.editorAddTagRepo.save(editorAddTag);
            return Body.getResponseBody(errorType);
        } else {
            return Body.getResponseBody(errorType);
        }
    }

    //删除某一图片标签
    public Body deleteTag(EditorDeleteTagDto editorDeleteTagDto) {
        int errorType = fv.validateDeleteTag(editorDeleteTagDto);

        if (errorType == 0) {
            EditorDeleteTag editorDeleteTag = FeedbackDto2Domain.transferEditorDeleteTag(editorDeleteTagDto);
            //TODO 检测 editorDeleteTag 完整性
            this.editorDeleteTagRepo.save(editorDeleteTag);
            return Body.getResponseBody(errorType);
        } else {
            return Body.getResponseBody(errorType);
        }
    }

    //给多个图片评分
    public Body addImgScore(EditorImageScoreDto editorImageScoreDto) {
        int errorType = fv.validateImageScore(editorImageScoreDto);

        if (errorType == 0) {
            List<EditorImageScore> editorImageScoreList = FeedbackDto2Domain.transferEditorImageScore(editorImageScoreDto);
            //TODO 检测 editorImageScoreList 完整性
            this.editorImageScoreRepo.save(editorImageScoreList);
            return Body.getResponseBody(errorType);
        } else {
            return Body.getResponseBody(errorType);
        }

    }
}
